//
//  TaskCell.swift
//  demo01
//
//  Created by babybus on 2025/6/30.
//
import Foundation
import UIKit
import SnapKit

class TaskCell:UICollectionViewCell{
    private var taskImage:UIImageView = UIImageView()   //任务图片
    private var taskName:UILabel = UILabel()            //任务名称
    private var taskDetail:UILabel = UILabel()          //任务描述	
    private var taskButton:MyButton = MyButton()        //任务按钮
    private var sideImage:UIImageView = UIImageView()   //侧边图片
    private var cellView:UIView = UIView()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:NSCoder) -- fatalError")
    }
    func configure(image:UIImage,nameOfTask:String,detailOfTask:String,numOfButton:Int=0){
        //设置头像
        taskImage.image = image
        taskImage.contentMode = .scaleAspectFit
        //任务名称和描述
        taskName.text = nameOfTask
        taskDetail.text = detailOfTask
        taskDetail.font = UIFont.systemFont(ofSize: 12)
        taskDetail.adjustsFontSizeToFitWidth = true // 开启自动缩放
        taskDetail.minimumScaleFactor = 0.5 // 最小缩放比例（默认0.5）
        taskDetail.textColor = .systemGray
        
        //任务按钮
        setButtom(num:numOfButton)
    }
    
    func setButtom(num:Int){
        if num > 0{
            taskButton.setTitle("+"+String(num), for: .normal)//按钮文本
            taskButton.setTitleColor(UIColor.systemOrange.withAlphaComponent(0.9), for: .normal)//按钮文本颜色
            taskButton.layer.borderColor = UIColor.systemOrange.withAlphaComponent(0.9).cgColor//边框颜色
            taskButton.setImage(UIImage(named: "./yellowDiamond")?.withTintColor(.systemYellow,renderingMode: .alwaysOriginal),for: .normal)//图片
            taskButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)//字体设置
        }else if num == 0{
            taskButton.setTitle("明日再来", for: .normal)//按钮文本
            taskButton.setTitleColor(UIColor.systemGray, for: .normal)//按钮文本颜色
            taskButton.layer.borderColor = UIColor.systemGray5.cgColor//边框颜色
            taskButton.titleLabel?.font = UIFont.systemFont(ofSize: 13)//字体设置
        }else{
            taskButton.setTitle(String(num.magnitude) + "日后再来", for: .normal)//按钮文本
            taskButton.setTitleColor(UIColor.systemGray, for: .normal)//按钮文本颜色
            taskButton.layer.borderColor = UIColor.systemGray5.cgColor//边框颜色
            taskButton.titleLabel?.font = UIFont.systemFont(ofSize: 10)//字体设置
        }
        
        taskButton.backgroundColor = .white//背景色
        taskButton.layer.borderWidth = 1.0//边框
        taskButton.layer.cornerRadius = 12//圆角
        taskButton.layer.masksToBounds = true
        taskButton.titleLabel?.adjustsFontSizeToFitWidth = true//字体缩放
        taskButton.titleLabel?.minimumScaleFactor = 0.25//字体缩放最小倍数
        
    }
    func setupUI(){
        //将任务名称和任务描述放到stack中
        let taskStack = UIStackView()
        taskStack.axis = .vertical
        taskStack.alignment = .fill
        taskStack.distribution = .fill
        
        taskName.textColor = .black
        taskDetail.textColor = .black

        //将图片和（任务名称+任务描述）放到stack中
        let leftStack = UIStackView()
        leftStack.axis = .horizontal
        leftStack.alignment = .fill
        leftStack.distribution = .fill
        leftStack.spacing = 16
        
        //      组装组件
        taskStack.addArrangedSubview(taskName)
        taskStack.addArrangedSubview(taskDetail)
        
        leftStack.addArrangedSubview(taskImage)
        leftStack.addArrangedSubview(taskStack)
        
        cellView.addSubview(leftStack)
        cellView.addSubview(taskButton)
        
        contentView.addSubview(cellView)
        //组件布局
        //  任务名称+任务描述+任务图片
        taskName.snp.makeConstraints { make in
            make.top.equalToSuperview().inset(0)
            make.height.equalTo(16)
            make.width.equalTo(199)
        }
        taskDetail.snp.makeConstraints { make in
            make.top.equalTo(taskName.snp.bottom).inset(5)
            make.height.equalTo(14)
//            make.width.equalTo(199)
            make.right.equalToSuperview()
        }
        taskImage.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.width.equalTo(24)
            make.height.equalTo(24)
            make.left.equalToSuperview().inset(2)
        }
        //  （任务名称+任务描述）stack + 左侧stack +按钮
        taskStack.snp.makeConstraints { make in
            make.height.equalToSuperview()
            make.top.equalToSuperview()
        }
        leftStack.snp.makeConstraints { make in
            make.left.equalToSuperview().inset(0)
            make.height.equalToSuperview()
            make.width.equalTo(200)
        }
        taskButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(12)
            make.centerY.equalToSuperview()
            make.width.equalTo(64)
            make.height.equalTo(26)
        }
        
        //最外层
        cellView.snp.makeConstraints { make in
            make.height.equalToSuperview()
            make.width.equalToSuperview()
        }
    }
    
}

